<?php

declare (strict_types=1);

namespace magein\pay;

class PayConfig
{


    /**
     * @var PayConfig|null
     */
    protected static $instance = null;

    /**
     * @var array
     */
    protected $config = [];

    protected function __construct()
    {

    }

    protected function __clone()
    {

    }

    /**
     * @return PayConfig
     */
    public static function instance(): PayConfig
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * @param $config
     * @return void
     */
    public function setConfig($config)
    {
        $this->config = $config;
    }

    /**
     * @param $key
     * @return mixed
     */
    public static function value($key)
    {
        return self::$instance->config[$key];
    }

    /**
     * @return string
     */
    public static function domain(): string
    {
        return trim(self::$instance->config['domain'] ?? '', '/');
    }

    /**
     * 获取channel配置文件
     * @param $channel_id
     * @return array|mixed
     */
    public static function channels($channel_id = null)
    {
        $channels = self::$instance->config['channels'] ?? [];

        if (!is_null($channel_id)) {
            return $channels[$channel_id] ?? [];
        }

        return $channels;
    }

    /**
     * 获取渠道账号配置
     * @param $channel_id
     * @param $platform_id
     * @return array
     */
    public static function channelAccount($channel_id, $platform_id = null): array
    {
        $channels = self::$instance->config['channels'] ?? [];

        $accounts = $channels[$channel_id]['accounts'] ?? [];

        if (!is_null($platform_id)) {
            return $accounts[$platform_id] ?? [];
        }

        return $accounts;
    }

    /**
     * @param $channel_id
     * @return mixed|string
     */
    public static function channelSecret($channel_id)
    {
        $channels = self::$instance->config['channels'] ?? [];

        return $channels[$channel_id]['secret'] ?? '';
    }

    /**
     * @param $platform_id
     * @return array|mixed
     */
    public static function platforms($platform_id = null)
    {
        $platforms = self::$instance->config['platforms'] ?? [];

        if (!is_null($platform_id)) {
            return $platforms[$platform_id] ?? [];
        }

        return $platforms;
    }

    /**
     * 获取平台账号配置文件
     * @param $platform_id
     * @param $account
     * @return array|mixed
     */
    public static function platformAccount($platform_id, $account = null)
    {
        $platforms = self::$instance->config['platforms'] ?? [];

        $accounts = $platforms[$platform_id]['accounts'] ?? [];

        if (!is_null($account)) {
            return $accounts[$account] ?? [];
        }

        return $accounts;
    }

    /**
     * @param $code
     * @return array|mixed|string
     */
    public static function codes($code = null)
    {
        $codes = self::$instance->config['codes'] ?? [];

        if (!is_null($code)) {
            return $codes[$code] ?? '';
        }

        return $codes;
    }

    /**
     * 扫码支付配置
     * @param $value
     * @return array|mixed|string
     */
    public static function qrcode($value = null)
    {
        $qrcode = self::$instance->config['qrcode'] ?? [];

        if (!is_null($value)) {
            return $qrcode[$value] ?? '';
        }

        return $qrcode;
    }

    /**
     * 获取场景配置
     * @param $value
     * @return array|mixed|string
     */
    public static function scenes($value = null)
    {
        $scenes = self::$instance->config['scenes'] ?? [];

        if (!is_null($value)) {
            foreach ($scenes as $scene) {
                if ($scene['value'] == $value) {
                    return $scene;
                }
            }
            return [];
        }

        return $scenes;
    }

    /**
     * extension配置
     * @param $extension
     * @return array|mixed|string
     */
    public static function extension($extension = null)
    {
        $extensions = self::$instance->config['extension'] ?? [];

        if (!is_null($extension)) {
            return $extensions[$extension] ?? '';
        }

        return $extensions;
    }
}